import 'package:kkhousekeep/models/base_respond_entity.dart';
import 'package:kkhousekeep/models/repair_fault_type_entity.dart';
import 'package:kkhousekeep/net/base_connect.dart';
import 'package:kkhousekeep/net/respnse_code.dart';
import 'package:logger_util/logger_util.dart';

import '../models/repair_order_detail_entity.dart';
import '../models/repair_order_entity.dart';
import '../models/repair_order_result_entity.dart';
import '../tools/dialog_util.dart';
import 'api.dart';

class HomeRepairConnect extends BaseConnect {
  Future<List<RepairOrderObj>> getRepairOrderList({required int tabId}) async {
    var response = await get(Api.getOrderList, query: {
      'state': tabId == 0 ? 'toBeRepaired' : 'completed',
    });
    if (response.body == null) {
      DialogUtil.showNetworkError();
      return Future.value([]);
    }
    RepairOrderEntity entity = RepairOrderEntity.fromJson(response.body);
    if (entity.code == ResponseCode.success) {
      return Future.value(entity.obj);
    }
    return Future.value([]);
  }

  Future<BaseRespondEntity?> submitOrder({
    required int area,
    required String address,
    required String committer,
    required String phone,
    required int urgency,
    required int faultTypeId,
    required String mark,
    required List<String> pics,
  }) async {
    if (area == -1 || urgency == -1) {
      logW("数据异常,请检查!");
      return Future.value();
    }

    var response = await post(Api.addOrder, {
      'area': area,
      'address': address,
      'committer': committer,
      'phone': phone,
      'urgency': urgency,
      'faultTypeId': faultTypeId,
      'mark': mark,
      'pics': pics,
    });
    if (response.body == null) {
      DialogUtil.showNetworkError();
      return Future.value();
    }
    BaseRespondEntity entity = BaseRespondEntity.fromJson(response.body);
    return Future.value(entity);
  }

  Future<BaseRespondEntity?> appraiseOrder({
    required int orderId,
    required int responseSpeed,
    required int serviceAttitude,
    required int constructionQuality,
  }) async {
    var response = await post(Api.appraiseOrder, {
      'workOrderId': orderId,
      'responseSpeed': responseSpeed,
      'serviceAttitude': serviceAttitude,
      'constructionQuality': constructionQuality,
    });
    if (response.body == null) {
      DialogUtil.showNetworkError();
      return Future.value();
    }
    BaseRespondEntity entity = BaseRespondEntity.fromJson(response.body);
    return Future.value(entity);
  }

  Future<List<RepairFaultTypeObj>> getPropertyFaultType() async {
    var response = await get(Api.getFaultType);
    if (response.body == null) {
      DialogUtil.showNetworkError();
      return Future.value([]);
    }
    RepairFaultTypeEntity entity =
        RepairFaultTypeEntity.fromJson(response.body);
    if (entity.code == ResponseCode.success) {
      return Future.value(entity.obj);
    }
    return Future.value([]);
  }

  Future<BaseRespondEntity?> cancelApply({required int orderId}) async {
    var response = await delete(Api.cancelOrder, query: {
      'id': '$orderId',
    });
    if (response.body == null) {
      DialogUtil.showNetworkError();
      return Future.value();
    }
    BaseRespondEntity entity = BaseRespondEntity.fromJson(response.body);
    return Future.value(entity);
  }

  Future<RepairOrderDetailObj> getRepairOrderDetail({
    required int orderId,
  }) async {
    var response = await get(Api.getOwnerOrderDetail, query: {
      'id': '$orderId',
    });
    if (response.body == null) {
      DialogUtil.showNetworkError();
      return Future.value(
        RepairOrderDetailObj().copyWith(
          worker: "",
          repairArea: -1,
          address: "",
          repairApplicant: "",
          phone: "",
          urgency: -1,
          fatalMark: "",
          fatalType: "",
          pics: [],
          time: "",
          state: "",
          acceptorPhone: "",
        ),
      );
    }
    RepairOrderDetailEntity entity =
        RepairOrderDetailEntity.fromJson(response.body);
    if (ResponseCode.success == entity.code) {
      return Future.value(entity.obj);
    }

    return Future.value(RepairOrderDetailObj());
  }

  Future<RepairOrderResultObj> getRepairOrderResult({
    required int orderId,
  }) async {
    var response = await get(Api.getOrderDealResult, query: {
      'id': '$orderId',
    });
    if (response.body == null) {
      DialogUtil.showNetworkError();
      return Future.value(
        RepairOrderResultObj().copyWith(
          fatalType: '',
          repairStandard: '',
          cause: "",
          result: '',
          propose: '',
          pics: [],
        ),
      );
    }
    RepairOrderResultEntity entity =
        RepairOrderResultEntity.fromJson(response.body);
    if (ResponseCode.success == entity.code) {
      return Future.value(entity.obj);
    }
    return Future.value(RepairOrderResultObj().copyWith(
      fatalType: '',
      repairStandard: '',
      result: '',
      propose: '',
      pics: [],
    ));
  }
}
